package com.sc.nft.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.ImGroup;
import com.sc.nft.entity.dto.TransferGroupMasterDTO;
import com.sc.nft.entity.vo.AdminGroupListVO;
import com.sc.nft.mapper.ImGroupMapper;
import com.sc.nft.sup.PageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ImGroupDao extends ServiceImpl<ImGroupMapper, ImGroup> {

    public Boolean groupIsExist(Long id) {
        QueryWrapper<ImGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dao_id", id);
        List<ImGroup> imGroups = baseMapper.selectList(queryWrapper);
        return CollUtil.isNotEmpty(imGroups) ? Boolean.TRUE : Boolean.FALSE;
    }

    public List<ImGroup> getByDaoIdGroup(Long id) {
        QueryWrapper<ImGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dao_id", id);
        queryWrapper.orderByDesc("sort");
        return baseMapper.selectList(queryWrapper);
    }

    public ImGroup getById(Long id) {
        QueryWrapper<ImGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return baseMapper.selectOne(queryWrapper);
    }

    public ImGroup getByGroupId(String groupId) {
        QueryWrapper<ImGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id", groupId);

        return baseMapper.selectOne(queryWrapper);
    }

    public void modifierGroup(String groupId, ImGroup imGroup) {
        UpdateWrapper<ImGroup> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("group_id", groupId);
        baseMapper.update(imGroup, queryWrapper);
    }

    public int deleteGroupId(String groupId) {
        QueryWrapper<ImGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id", groupId);
        queryWrapper.eq("official", 0);
        int delete = baseMapper.delete(queryWrapper);
        return delete;
    }

    public Page<AdminGroupListVO> adminGroupList(PageRequest request) {
        return baseMapper.adminGroupList(request.toMp());
    }

    /**
     * 转让群主
     *
     * @param transferGroupMasterDTO
     */
    public void transferGroupMaster(TransferGroupMasterDTO transferGroupMasterDTO) {
        baseMapper.transferGroupMaster(transferGroupMasterDTO);
    }

    public List<ImGroup> getStartAndEndData(Integer start, Integer end) {
        return baseMapper.getStartAndEndData(start, end);
    }

    public ImGroup getOfficialLimitFinallyData() {
        return baseMapper.getOfficialLimitFinallyData();
    }

    /**
     * 获取所有官方群的数量
     *
     * @return
     */
    public Integer getGroupOfficialSum() {
        return baseMapper.getGroupOfficialSum();
    }

    /**
     * 查询某个群是否存在
     * @param groupId
     * @return
     */
    public Boolean isOfficial(String groupId) {
        QueryWrapper<ImGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupId);
        wrapper.eq("official", 1);
        Integer integer = baseMapper.selectCount(wrapper);
        return integer > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

}
